package com.example.widgetappone;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;

import java.util.Arrays;

public class AppWidgetProvider extends android.appwidget.AppWidgetProvider {
    private static final String TAG = "AppWidgetProvider";
    private static AppWidgetProvider sInstance;

    static synchronized AppWidgetProvider getInstance() {
        if (sInstance == null) {
            sInstance = new AppWidgetProvider();
        }
        return sInstance;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        String action = intent.getAction();
        Log.e(TAG, "onReceive " + intent.getAction());
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Log.e(TAG, "onUpdate appWidgetManager " + appWidgetManager.toString() + "//appWidgetIds " + appWidgetIds);
        defaultAppWidget(context, appWidgetIds);
    }

    /**
     * set up default app widget data
     *
     * @param context
     * @param appWidgetIds
     */
    private void defaultAppWidget(Context context, int[] appWidgetIds) {
        final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_app);
        remoteViews.setTextViewText(R.id.txt, "循环初始化");
        remoteViews.setImageViewResource(R.id.img, R.mipmap.ic_launcher);
        linkButtons(context, remoteViews);
        pushUpdate(context, appWidgetIds, remoteViews);
    }

    /**
     * update widget from server
     *
     * @param widgetService
     * @param appWidgetIds
     * @param remoteViews
     */
    private void updateWidget(WidgetService widgetService, int[] appWidgetIds, RemoteViews remoteViews) {
        linkButtons(widgetService, remoteViews);
        pushUpdate(widgetService, appWidgetIds, remoteViews);
    }

    /**
     * bind pendingintent to remoteviews
     *
     * @param context
     * @param remoteViews
     */
    private void linkButtons(Context context, RemoteViews remoteViews) {
        Intent intentStop = new Intent(WidgetService.STOP_ACTION);
        Intent intentStart = new Intent(WidgetService.START_ACTION);

        intentStop.setClass(context, WidgetReceiver.class);
        intentStart.setClass(context, WidgetReceiver.class);

        PendingIntent pendingIntentStop = PendingIntent.getBroadcast(context, 0, intentStop, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent pendingIntentStart = PendingIntent.getBroadcast(context, 0, intentStart, PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setOnClickPendingIntent(R.id.btn_stop, pendingIntentStop);
        remoteViews.setOnClickPendingIntent(R.id.btn_start, pendingIntentStart);
    }

    /**
     * refresh widgets ui
     *
     * @param context
     * @param appWidgetIds
     * @param remoteViews
     */
    private void pushUpdate(Context context, int[] appWidgetIds, RemoteViews remoteViews) {
        // Update specific list of appWidgetIds if given, otherwise default to all
        final AppWidgetManager gm = AppWidgetManager.getInstance(context);
        Log.d(TAG, "pushUpdate: appWidgetIds = " + (appWidgetIds == null ? null : Arrays.toString(appWidgetIds)));
        if (appWidgetIds != null) {
            gm.updateAppWidget(appWidgetIds, remoteViews);
        } else {
            gm.updateAppWidget(new ComponentName(context, this.getClass()), remoteViews);
        }
    }

    /**
     * notity ui change from serice
     *
     * @param service
     */
    public void notifyChange(WidgetService service) {
        final RemoteViews remoteViews = new RemoteViews(service.getPackageName(), R.layout.widget_app);
        remoteViews.setTextViewText(R.id.txt, service.getLoopText());
        remoteViews.setImageViewResource(R.id.img, (service.getI() % 2 == 0) ? R.mipmap.ic_launcher : R.mipmap.ic_apple);
        updateWidget(service, null, remoteViews);
    }

    @Override
    public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
        Log.e(TAG, "onAppWidgetOptionsChanged " + appWidgetManager.toString() + "//appWidgetId " + appWidgetId);
        //do nothing
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        Log.e(TAG, "onDeleted appWidgetIds " + appWidgetIds);
        //do nothing
    }

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
        Log.e(TAG, "onEnabled");
        //do nothing
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
        Log.e(TAG, "onDisabled");
        //do nothing
    }

    @Override
    public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
        super.onRestored(context, oldWidgetIds, newWidgetIds);
        Log.e(TAG, "onRestored oldWidgetIds " + oldWidgetIds + "//newWidgetIds " + newWidgetIds);
        //do nothing
    }
}
